package com.tongann.wind.controller;

import com.tongann.wind.model.Register;
import com.tongann.wind.model.User;
import com.tongann.wind.service.UserService;
import com.tongann.wind.utils.Utils;
import org.apache.tomcat.util.security.MD5Encoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Controller
public class LoginController {

    @Autowired
    UserService userService;

    @GetMapping("/signup")
    public  String signup(HttpSession session)
    {
        session.setAttribute("loginUser", "xxx");
        return "redirect:/signup.html";
    }

    @GetMapping("/login")
    public String toLogin()
    {
        return "login";
    }

    @PostMapping("/register")
    public String register(Register register , Model model, HttpSession session)
    {
            if (register.getUsername() == null || "".equals(register.getUsername().trim()))
            {
                model.addAttribute("msg", "手机号为空");
                return "signup";
            }
            else  if (register.getPassword() == null || "".equals(register.getPassword().trim()))
            {
                model.addAttribute("msg", "密码为空");
                return "signup";
            }
            else  if (register.getRePassword() == null || "".equals(register.getRePassword().trim()))
            {
                model.addAttribute("msg", "再次输入的密码为空");
                return "signup";
            }
            else  if (!register.getPassword().equals(register.getRePassword()))
            {
                model.addAttribute("msg", "两次输入的密码不一致");
                return "signup";
            }
            else
            {
                String salt = Utils.radom6num();
                String pass = register.getPassword() + salt;
                pass = DigestUtils.md5DigestAsHex(pass.getBytes());

                User user = new User();
                user.setUsername(register.getUsername());
                user.setPassword(pass);
                user.getOrganization().setName(register.getOrganization());
                String uuid = UUID.randomUUID().toString().replace("-", "");
                user.setUuid(uuid);
                user.setSalt(salt);
                user.setRegisterTime(new Date());

                System.out.println(user);
                userService.addNewUser(user);
                session.setAttribute("loginUser", register.getUsername());
                return "redirect:/index.html";
            }

    }



    @PostMapping("/login")
    public String doLogin(User user , Model model , HttpSession session)
    {
        if (user.getUsername() == null || "".equals(user.getUsername().trim())) {
            model.addAttribute("msg", "用户名为空");
            return "login";
        } else if (user.getPassword() == null || "".equals(user.getPassword().trim())) {
            model.addAttribute("msg", "密码为空");
            return "login";
        } else {
            User dbUser = userService.getUserByUsername(user.username);
            if (dbUser == null) {
                model.addAttribute("msg", "用户名错误");
                return "login";
            } else {
                String permit = DigestUtils.md5DigestAsHex((user.getPassword() + dbUser.salt).getBytes());
                if (permit.equals(dbUser.password)) {
                    session.setAttribute("loginUser", user.getUsername());
                    return "redirect:/index.html";
                } else {
                    model.addAttribute("msg", "密码错误");
                    return "login";
                }
            }
        }
    }

    @ResponseBody
    @RequestMapping("/user")
    public List<User> getAllUser() {
        return userService.getAllUser();
    }

}
